<template>
  <div class="app-container">

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="item in fList" :key="item.id">
        <span slot="label">{{ item.name + '(' +item.langName +')' }}</span>
        <el-button
          type="text"
          icon="el-icon-edit"
          size="mini"
          slot="label"
          @click="handleUpdateFather(item)"
        ></el-button>
      </el-tab-pane>
      <el-tab-pane name="xz">
        <el-button
          type="text"
          plain
          icon="el-icon-plus"
          size="mini"
          slot="label"
          @click="handleAdd"
        >新增</el-button>
      </el-tab-pane>
    </el-tabs>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddChildren"
        >新增分类</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="batchUpdate"
        >批量修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table stripe v-loading="loading" :data="cList">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="站点" align="center" prop="siteId" />-->
      <el-table-column label="语言" align="center" prop="langName" />
      <el-table-column label="分类名称" align="center" prop="name" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" />
        </template>
      </el-table-column>
<!--      <el-table-column label="样式" align="center" prop="style" />-->
<!--      <el-table-column label="类型" align="center" prop="type" />-->
<!--      <el-table-column label="图片" align="center" prop="image" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="父ID" align="center" prop="pid" />-->
      <el-table-column label="排序" align="center" prop="weigh" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.weigh" />
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" >
        <template slot-scope="scope">
          <el-input v-model="scope.row.description" />
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="status" />-->
      <el-table-column label="创建时间" align="center" prop="createtime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatetime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 'normal'"
            type="text"
            size="mini"
            class="normalButton"
            @click="handleUpdate(scope.row,'hidden')"
          >隐藏</el-button>
          <el-button
            v-else-if="scope.row.status == 'hidden'"
            type="text"
            size="mini"
            @click="handleUpdate(scope.row,'normal')"
          >显示</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->

    <!-- 添加或修改商城分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body
               :close-on-click-modal="false" :close-on-press-escape="false" :before-close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="站点" prop="siteId">-->
<!--          <el-input v-model="form.siteId" placeholder="请输入站点" />-->
<!--        </el-form-item>-->
        <el-form-item label="语言" prop="langId" class="oneItem">
<!--          <el-input v-model="form.langId" placeholder="请输入语言" />-->
<!--          <el-radio-group v-model="radioModel" class="radioGroupClass">-->

            <el-radio
              v-for="item in langList"
              v-model="radioModel"
              class="radioItemClass"
              :label="item.id"
            >{{item.langCn}}</el-radio>


<!--          </el-radio-group>-->
        </el-form-item>

        <el-form-item label="分类类型" prop="name">
          <el-radio v-model="form.type" label="video" >视频</el-radio>
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
<!--        <el-form-item label="样式:1=一级分类,2=二级分类,3=三级分类" prop="style">-->
<!--          <el-input v-model="form.style" placeholder="请输入样式:1=一级分类,2=二级分类,3=三级分类" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="图片" prop="image">-->
<!--          <image-upload v-model="form.image"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="权重" prop="weigh">-->
<!--          <el-input v-model="form.weigh" placeholder="请输入权重" />-->
<!--        </el-form-item>-->
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="form.pid != 0 || form.pid == ''" @click="cancel">取 消</el-button>
        <el-button v-if="form.pid == 0 && form.pid != ''" type="danger" plain @click="handleDelete(form)">删 除</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fatherList, childrenList, getCategory, delCategory, addCategory, updateCategory, batchUpdate } from "@/api/video/category";
import { listLang } from "@/api/video/lang";

export default {
  name: "Category",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 标签页
      activeName: null,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城父分类表格数据
      fList: [],
      // 商城子分类表格数据
      cList: [],
      // 子分类对照表格数据
      ccList: [],
      // 语言列表
      langList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        siteId: null,
        langId: null,
        name: null,
        style: null,
        type: null,
        image: null,
        pid: null,
        weigh: null,
        description: null,
        status: null,
        createtime: null,
        updatetime: null
      },
      // 表单参数
      form: {},
      // 定义个单选框的model
      radioModel: '1',
      // 表单校验
      rules: {
        // siteId: [
        //   { required: true, message: "站点不能为空", trigger: "blur" }
        // ],
        // langId: [
        //   { required: true, message: "语言不能为空", trigger: "blur" }
        // ],
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        // style: [
        //   { required: true, message: "样式:1=一级分类,2=二级分类,3=三级分类不能为空", trigger: "blur" }
        // ],
        // type: [
        //   { required: true, message: "类型:video=视频,year=年份,area=地区不能为空", trigger: "blur" }
        // ],
        // image: [
        //   { required: true, message: "图片不能为空", trigger: "blur" }
        // ],
        // pid: [
        //   { required: true, message: "父ID不能为空", trigger: "blur" }
        // ],
        weigh: [
          { required: true, message: "权重不能为空", trigger: "blur" }
        ],
        // description: [
        //   { required: true, message: "描述不能为空", trigger: "blur" }
        // ],
        // status: [
        //   { required: true, message: "状态不能为空", trigger: "change" }
        // ],
      }
    };
  },
  created() {
    this.fatherList();
    this.getLangList();
  },
  methods: {
    /** 切换页签触发事件 */
    handleClick() {
      if (this.activeName != 'xz') {
        this.queryParams.pid = Number(this.fList[this.activeName].id);
        this.childrenList();
      }
    },
    /** 查询商城父分类列表 */
    fatherList() {
      this.loading = true;
      fatherList().then(response => {
        if(response.rows.length != 0) {
          this.fList = response.rows;
          this.queryParams.pid = Number(this.fList[this.activeName].id)
          this.childrenList();
        } else {
          this.fList = []
        }
        this.loading = false;
      });
    },
    /** 查询商城子分类列表 */
    childrenList() {
      this.loading = true;
      childrenList(this.queryParams).then(response => {
        if(response.rows.length != 0) {
          this.cList = response.rows;
          this.ccList = JSON.parse(JSON.stringify(response.rows));
        } else {
          this.cList = []
          this.ccList = []
        }
        this.loading = false;
      });
    },
    getLangList() {
      this.loading = true;
      listLang().then(response => {
        this.langList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.radioModel = null;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        langId: null,
        name: null,
        style: null,
        type: null,
        image: null,
        pid: null,
        weigh: null,
        description: null,
        status: null,
        createtime: null,
        updatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getList();
    // },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.radioModel = this.fList[this.activeName].langId+'';
      this.title = "添加父级分类";
      this.form.type = 'video';
      this.form.pid = '';
      this.open = true;
    },
    /** 新增按钮操作 */
    handleAddChildren() {
      this.reset();
      this.radioModel = this.fList[this.activeName].langId+'';
      this.form.type = 'video';
      this.title = "添加子级分类";
      this.form.pid = this.fList[this.activeName].pid;
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdateFather(row) {
      this.reset();
      this.form = row;
      this.title = "修改商城分类";
      this.radioModel = this.fList[this.activeName].langId+'';
      this.open = true;
    },
    handleUpdate(row,str) {
      row.status = str
      updateCategory(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.fatherList();
      });
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.langId = this.radioModel;
          if (this.form.id != null) {
            this.form.updatetime = new Date().getTime();
            updateCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.radioModel = null;
              this.fatherList();
            });
          } else {
            this.form.createtime = new Date().getTime();
            addCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.radioModel = null;
              this.fatherList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除商城分类编号为"' + ids + '"的数据项？').then(function() {
        return delCategory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('system/category/export', {
    //     ...this.queryParams
    //   }, `category_${new Date().getTime()}.xlsx`)
    // },
    batchUpdate() {
      const modified = [];
      // 找出被修改的元素
      for (let i = 0; i < this.cList.length; i++) {
        if (this.cList[i] !== this.ccList[i]) {
          modified.push(this.cList[i])
        }
      }
      console.log(modified)
      if(modified.length == 0) {
        this.$modal.msgSuccess("请先修改数据再提交！");
        return;
      }
      batchUpdate(modified).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.fatherList();
      });
    },
  }
};
</script>
<style>
.oneItem {
  margin-bottom: 0px;
}
.normalButton {
  color: #727272;
}
.radioGroupClass {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
}
.radioItemClass {
  width: 84px;
  height: 35px;
}
</style>
